<?php
session_start(); //we need to call PHP's session object to access it through CI
class Home extends CI_Controller {

	function __construct()
	{
		parent::__construct();
	}

	function index()
	{
		if($this->session->userdata('user')){
		
		date_default_timezone_set('Asia/Pontianak');
		$this->load->model('m_kelas_ujian');
		
			$session_data = $this->session->userdata('user');
			$this->data['title'] = 'Home';
			$this->data['id'] = $session_data['id'];
			$this->data['username'] = $session_data['username'];
			$this->data['nama'] = $session_data['nama'];
			$this->load->library('view');
			$this->view->set_menu('home');
			$this->view->set_portlet_title('Menu Siswa');
			$this->view->set_portlet_style('portlet box red');
			$this->view->set_view('siswa/siswa_dashboard');
			
			
			$uj_id = $this->m_kelas_ujian->get_ujian_with_id($this->data['id']);
			$rows = $this->m_kelas_ujian->get_ujian_by_kelas($this->data['username'], count($uj_id)?$uj_id:null);
			$i = 0;
			foreach ($rows as $row){
				$from = $row->uj_date.' '.$row->uj_start;
				$to = $row->uj_date.' '.$row->uj_end;
				$rows[$i]->start = $this->date_is_between($from, $to);
				$i++;
			}
			$this->data['ujian'] = $rows;
		
			
			
			$this->view->show($this->data);
		}else{
			redirect('user/login', 'refresh');
		}
	}
	
		function date_is_between($from, $to, $date = 'now') {
		$date = is_int($date) ? $date : strtotime($date); // convert non timestamps
		$from = is_int($from) ? $from : strtotime($from); // ..
		$to = is_int($to) ? $to : strtotime($to);         // ..
		if(($date >= $from) && ($date <= $to)) return 1;
		else if($date <= $from) return 2;
		else return 0; // extra parens for clarity
	}

	function logout()
	{
		$this->session->unset_userdata('user');
		session_destroy();
		redirect('user/login', 'refresh');
	}

	

	
	}
?>